'use strict'
const express = require('express');

const util = require('../util');
const config = require('../config');


const router = express.Router();
const logger = util.logger;

const PUBLIST_SELF = './shell/publish-self.sh'


router.get('/test', (req, res) => {
  res.end('success')
})

router.post('/push', (req, res) => {
  let body = req.body;
  logger.info('push data: ', body);
  logger.debug('config self: ', config.hooks_self);
  if (body.password === config.hooks_self.password
    && body.ref === config.hooks_self.ref) {
    logger.debug('开始更新..')

    //运行自动更新脚本,不需要同步执行
    let {stdout, stderr} = util.runShell(PUBLIST_SELF);
    logger.info('publish-self stdout: %s', stdout)
    logger.info('publish-self stderr: %s', stderr)
  }
  res.end('success');
});

module.exports = router;